package com.crm.backend.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.crm.backend.domain.entity.RestBean;
import com.crm.backend.domain.po.CustomerLoss;
import com.crm.backend.domain.po.Services;
import com.crm.backend.service.ICustomerLossService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDate;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author dyy
 * @since 2025-05-29
 */
@RestController
@RequestMapping("/api/v1/customer-loss")
public class CustomerLossController {

    @Autowired
    private ICustomerLossService customerLossService;

    /**
     * 自动检测客户流失预警
     * @return RestBean封装的响应结果
     */
    @PostMapping("/warning")
    public RestBean<Void> detectCustomerLossWarning() {
        // 调用服务层方法进行自动检测
//        customerLossService.detectLossWarning();

        //异步调用

        //待创建
        return RestBean.success();
    }

    /**
     * 新增客户流失记录
     * @param customerLoss 客户流失记录信息
     * @return RestBean封装的响应结果
     */
    @PostMapping
    public RestBean<CustomerLoss> addCustomerLoss(@RequestBody CustomerLoss customerLoss) {
        boolean savedCustomerLoss = customerLossService.save(customerLoss);
        return RestBean.success();
    }

    /**
     * 修改客户流失记录
     * @param id 客户流失记录编号
     * @param customerLoss 修改后的客户流失记录信息
     * @return RestBean封装的响应结果
     */
    @PutMapping("/{id}")
    public RestBean<CustomerLoss> updateCustomerLoss(@PathVariable(value = "id") Integer id, @RequestBody CustomerLoss customerLoss) {
        customerLoss.setClId(id);
        boolean updatedCustomerLoss = customerLossService.updateById(customerLoss);
        if (updatedCustomerLoss) {
            return RestBean.success();
        } else {
            return RestBean.failure(404, "客户流失记录未找到");
        }
    }

    /**
     * 删除客户流失记录
     * @param id 客户流失记录编号
     * @return RestBean封装的响应结果
     */
    @DeleteMapping("/{id}")
    public RestBean<Void> deleteCustomerLoss(@PathVariable(value = "id") Integer id) {
        boolean deleted = customerLossService.removeById(id);
        if (deleted) {
            return RestBean.success();
        } else {
            return RestBean.failure(404, "客户流失记录未找到");
        }
    }

    /**
     * 查询客户流失记录
     * @param page 页码
     * @param size 每页大小
     * @return RestBean封装的响应结果
     */
    @GetMapping("/page")
    public RestBean<IPage<CustomerLoss>> getCustomerLosses(@RequestParam(value = "page",defaultValue = "1") Integer page,
                                                           @RequestParam(value = "size",defaultValue = "10") Integer size) {

        Page<CustomerLoss> pageQuery = new Page<>(page, size);
        // 创建查询条件
        LambdaQueryWrapper<CustomerLoss> queryWrapper = new LambdaQueryWrapper<>();
        // 如果状态不为空，则添加状态过滤条件
//        if ( clLossTime!= null) {
//            LocalDate parse = LocalDate.parse(clLossTime);
//            queryWrapper.eq(CustomerLoss::getClLossTime, parse);
//        }
        // 执行分页查询
        IPage<CustomerLoss> customerLossPage = customerLossService.page(pageQuery, queryWrapper);
        // 封装分页结果并返回
        return RestBean.success(customerLossPage);
    }
}